import requests
import time
import tkinter as tk
from tkinter import messagebox, ttk, scrolledtext

print("123")

def update_progress(progress_var):
    """模拟进度条更新"""
    for i in range(101):
        time.sleep(0.05)  # 模拟耗时操作
        progress_var.set(i)
        root.update_idletasks()  # 强制刷新界面

def validate_inputs():
    """校验所有必填字段"""
    # 获取各控件值
    cookie = entry_cookie.get('1.0', tk.END).strip()
    keyword = entry_word.get().strip()
    days = combo_days.get()
    
    # 校验逻辑
    if not cookie:
        messagebox.showerror("错误", "小红书Cookie不能为空！")
        entry_cookie.focus_set()
        return False
    
    if not keyword:
        messagebox.showerror("错误", "搜索关键词不能为空！")
        entry_word.focus_set()
        return False
    
    if not days:
        messagebox.showerror("错误", "请选择最近天数！")
        combo_days.focus_set()
        return False
    
    return True

def run_spider():
    if not validate_inputs():
        return  # 校验失败则停止执行

    btn_run.config(state=tk.DISABLED)
    print(f"cookie:{entry_cookie.get('1.0', tk.END).strip()}")
    print(f"输入关键词:{entry_word.get()}")
    print(f"输入天数:{type(combo_days.get())}")
    # messagebox.showinfo("标题", "这是提示信息")

    # 创建进度条
    progress_var = tk.DoubleVar()
    print(f"progress_var:{progress_var}")
    progress_bar = ttk.Progressbar(root, variable=progress_var, maximum=100, length=280)
    progress_bar.grid(row=4, columnspan=2, pady=10)
    
    # 创建进度标签
    progress_label = tk.Label(root, text="开始爬取...")
    progress_label.grid(row=5, columnspan=2)

    update_progress(progress_var)
    
    progress_label.config(text="爬取完成！")
    btn_run.config(state=tk.NORMAL)



root = tk.Tk()
root.title("小红书爆文点赞自动分层工具")
root.geometry("400x350")  # 设置窗口宽度400像素，高度300像素
# 输入控件

tk.Label(root, text="小红书Cookie:", fg="red").grid(row=0, column=0, padx=5, pady=5)
entry_cookie = scrolledtext.ScrolledText(root, width=30, height=10)
entry_cookie.grid(row=0, column=1, padx=5, pady=5, sticky="ew")

tk.Label(root, text="搜索关键词:").grid(row=1, column=0, padx=5, pady=5)
entry_word = tk.Entry(root, width=30)
entry_word.insert(1, "123")
entry_word.grid(row=1, column=1, padx=5, pady=5, sticky="ew")

days_options = ["7", "3", "14", "30"]  # 可选项列表
tk.Label(root, text="最近天数:").grid(row=2, column=0, padx=5, pady=5)
combo_days = ttk.Combobox(root, values=days_options, width=28)  # width单位是字符数
combo_days.grid(row=2, column=1, padx=5, pady=5, sticky="ew")
combo_days.current(3)  # 默认选中30天

# 运行按钮
btn_run = tk.Button(root, text="开始爬取", command=run_spider)
btn_run.grid(row=3, columnspan=2, pady=10)

root.mainloop()






